import asyncRoutes from '@/router/asyncRoutes'
import constantRoutes from '@/router/constantRoutes'

const actions = {

  getRoutes(context, { firseRoutesPermission, secondRoutesPermission }) {
    if (firseRoutesPermission.includes('*')) {
      context.commit('updateRoutes', asyncRoutes)
      return asyncRoutes
    } else {
      const firseResultRoutes = asyncRoutes.filter(item => firseRoutesPermission.includes(item.permission))
      const resultRoutes = firseResultRoutes.map(item => {
        return {
          ...item,
          children: item.children.filter(route => secondRoutesPermission.includes(route.permission))
        }
      })
      context.commit('updateRoutes', resultRoutes)
      return resultRoutes
    }
  }
}
const mutations = {
  updateRoutes(state, resultRoutes) {
    state.routes = [...constantRoutes, ...resultRoutes]
  },
  updateButtons(state, buttons) {
    state.buttons = buttons
  }
}
const state = () => {
  return {
    routes: [],
    buttons: []
  }
}
const getters = {}
export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
